package com.wn26.shopczpro.servlet;

import java.io.IOException;
import java.sql.SQLException;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.wn26.shopczpro.entity.User;
import com.wn26.shopczpro.service.UserService;
import com.wn26.shopczpro.service.UserServiceImpl;

@WebServlet(name = "UserLoginServLet",urlPatterns = {"/userLogin"})
public class UserLoginServlet extends HttpServlet {
	
	private static final long SerialVersionUID = 1L;
	
	public UserLoginServlet() {
		super();
	}
	
	@Override
	protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws IOException, ServletException  {
		req.setCharacterEncoding("utf-8");
		resp.setContentType("text/html;charset=utf-8");
		
		String userName = req.getParameter("username");
		String userPwd = req.getParameter("userpwd");
		System.out.println(userName);
		UserServiceImpl usi = new UserServiceImpl();
		User loginUser = usi.userLoginService(userName, userPwd);
		System.out.println(loginUser);
		if(loginUser != null) {
			req.getRequestDispatcher("index.html").forward(req, resp);
		}else {
			req.getRequestDispatcher("register.html").forward(req, resp);
		}
	}	
		
	@Override
	protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws IOException, ServletException {
		doGet(req, resp);
	}

}
